﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ModBot.ConfigBuilder.Providers;

namespace ModBot.ConfigBuilder.Forms
{
    public partial class MainForm : Form
    {
        

        public MainForm()
        {
            InitializeComponent();
            InitializeClasses();
            InitializeHeals();
            InitializeBuffs();
            InitializeDebuffs();

            InitializeToolTips();
            InitializeSpells();

            settingsProvider = new ModBot.ConfigBuilder.Providers.ModBotSettingsProvider();
            
            SetupHealsDataGridView();
            SetupBuffsDataGridView();
            SetupDebuffsDataGridView();
            SetupEventsDataGridView();
        }

        #region Initialization
        private void InitializeClasses()
        {
            /*myClassCombobox.DataSource = Enum.GetNames(typeof(SharedLib.ClassName));
            myClassCombobox.Text = SharedLib.MyClass;
            Globals.MyClass = (SharedLib.ClassName)Enum.Parse(typeof(SharedLib.ClassName), SharedLib.MyClass);
            */
        }

        private void InitializeHeals()
        {
        }

        private void InitializeBuffs()
        {

        }

        private void InitializeDebuffs()
        {

        }

        private void InitializeToolTips()
        {
            ToolTip toolTip = new ToolTip();
            toolTip.IsBalloon = true;
            toolTip.UseAnimation = true;
            toolTip.UseFading = true;

            #region Settings Controls
            toolTip.SetToolTip(meleeCheckbox, SharedLib.GetSummaryForProperty(typeof(ModBotSettingsProvider), "DoMelee"));
            toolTip.SetToolTip(healsCheckbox, SharedLib.GetSummaryForProperty(typeof(ModBotSettingsProvider), "DoHeals"));
            toolTip.SetToolTip(buffsCheckbox, SharedLib.GetSummaryForProperty(typeof(ModBotSettingsProvider), "DoBuffs"));
            toolTip.SetToolTip(debuffsCheckbox, SharedLib.GetSummaryForProperty(typeof(ModBotSettingsProvider), "DoDebuffs"));
            toolTip.SetToolTip(eventsCheckbox, SharedLib.GetSummaryForProperty(typeof(ModBotSettingsProvider), "DoEvents"));
            toolTip.SetToolTip(curesCheckbox, SharedLib.GetSummaryForProperty(typeof(ModBotSettingsProvider), "DoCures"));
            toolTip.SetToolTip(sitCheckbox, SharedLib.GetSummaryForProperty(typeof(ModBotSettingsProvider), "DoSit"));
            toolTip.SetToolTip(lootCheckbox, SharedLib.GetSummaryForProperty(typeof(ModBotSettingsProvider), "DoLoot"));
            toolTip.SetToolTip(afkCheckbox, SharedLib.GetSummaryForProperty(typeof(ModBotSettingsProvider), "DoAfk"));
            toolTip.SetToolTip(pullCheckbox, SharedLib.GetSummaryForProperty(typeof(ModBotSettingsProvider), "DoPull"));
            toolTip.SetToolTip(deathSlotCheckbox, SharedLib.GetSummaryForProperty(typeof(ModBotSettingsProvider), "DeathSlot"));
            toolTip.SetToolTip(fwCheckbox, SharedLib.GetSummaryForProperty(typeof(ModBotSettingsProvider), "DoFW"));

            toolTip.SetToolTip(petCheckbox, SharedLib.GetSummaryForProperty(typeof(ModBotSettingsProvider), "DoPet"));
            toolTip.SetToolTip(petSpellTextbox, SharedLib.GetSummaryForProperty(typeof(ModBotSettingsProvider), "PetCast"));
            toolTip.SetToolTip(petFociTextbox, SharedLib.GetSummaryForProperty(typeof(ModBotSettingsProvider), "PetFoci"));
            toolTip.SetToolTip(petShrinkCheckbox, SharedLib.GetSummaryForProperty(typeof(ModBotSettingsProvider), "PetShrink"));
            toolTip.SetToolTip(petShrinkSpellTextbox, SharedLib.GetSummaryForProperty(typeof(ModBotSettingsProvider), "PetShrinkSpell"));
            toolTip.SetToolTip(petAggroCheckbox, SharedLib.GetSummaryForProperty(typeof(ModBotSettingsProvider), "PetAggro"));
            toolTip.SetToolTip(petAssistCheckbox, SharedLib.GetSummaryForProperty(typeof(ModBotSettingsProvider), "PetAssist"));

            toolTip.SetToolTip(forageCheckbox, SharedLib.GetSummaryForProperty(typeof(ModBotSettingsProvider), "DoForage"));
            toolTip.SetToolTip(forageIniTextbox, SharedLib.GetSummaryForProperty(typeof(ModBotSettingsProvider), "ForageIni"));

            toolTip.SetToolTip(mountCheckbox, SharedLib.GetSummaryForProperty(typeof(ModBotSettingsProvider), "DoMount"));
            toolTip.SetToolTip(mountCastTextbox, SharedLib.GetSummaryForProperty(typeof(ModBotSettingsProvider), "MountCast"));

            toolTip.SetToolTip(masterListTextbox, SharedLib.GetSummaryForProperty(typeof(ModBotSettingsProvider), "MasterList"));
            toolTip.SetToolTip(tankNameTextbox, SharedLib.GetSummaryForProperty(typeof(ModBotSettingsProvider), "TankName"));
            toolTip.SetToolTip(radiusTextbox, SharedLib.GetSummaryForProperty(typeof(ModBotSettingsProvider), "Radius"));
            toolTip.SetToolTip(sitAggroTextbox, SharedLib.GetSummaryForProperty(typeof(ModBotSettingsProvider), "SitAggroRadiusCheck"));
            toolTip.SetToolTip(afkMessageTextbox, SharedLib.GetSummaryForProperty(typeof(ModBotSettingsProvider), "AfkMessage"));
            toolTip.SetToolTip(networkIniTextbox, SharedLib.GetSummaryForProperty(typeof(ModBotSettingsProvider), "NetworkIni"));
            toolTip.SetToolTip(followStickTextbox, SharedLib.GetSummaryForProperty(typeof(ModBotSettingsProvider), "FollowStick"));
            toolTip.SetToolTip(followDistanceTextbox, SharedLib.GetSummaryForProperty(typeof(ModBotSettingsProvider), "FollowDistance"));
            #endregion

            #region Melee Controls
            toolTip.SetToolTip(meleeOffTankCheckbox, SharedLib.GetSummaryForProperty(typeof(MeleeSettingsProvider), "OffTank"));
            toolTip.SetToolTip(meleeLeashTextbox, SharedLib.GetSummaryForProperty(typeof(MeleeSettingsProvider), "ACLeash"));
            toolTip.SetToolTip(meleeAssistPctTextbox, SharedLib.GetSummaryForProperty(typeof(MeleeSettingsProvider), "ACAssistPct"));
            toolTip.SetToolTip(meleeAnnounceTextbox, SharedLib.GetSummaryForProperty(typeof(MeleeSettingsProvider), "ACAnnounce"));
            toolTip.SetToolTip(meleeMeleeTextbox, SharedLib.GetSummaryForProperty(typeof(MeleeSettingsProvider), "ACMeleeCmd"));
            toolTip.SetToolTip(meleeBeforeTextbox, SharedLib.GetSummaryForProperty(typeof(MeleeSettingsProvider), "ACBefore"));
            toolTip.SetToolTip(meleeAfterTextbox, SharedLib.GetSummaryForProperty(typeof(MeleeSettingsProvider), "ACAfter"));
            toolTip.SetToolTip(meleeManaPctTextbox, SharedLib.GetSummaryForProperty(typeof(MeleeSettingsProvider), "ACManaPct"));
            #endregion

            #region Heals Controls
            // ToDo : Add heals tooltips
            #endregion

            #region Buffs Controls
            // ToDo : Add buff tooltips
            #endregion

        }

        private void InitializeSpells()
        {
            if (SharedLib.SpellFilePath != null && SharedLib.SpellFilePath.Length > 0)
            {
                try
                {
                    Globals.SpellDatabaseProvider = SpellDatabaseProvider.ParseSpellsFromFile(SharedLib.SpellFilePath);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error parsing spell database. Please ensure you have selected a valid database file for loading.\r\n\r\n" + ex.Message, 
                        "Parser error", MessageBoxButtons.OK, MessageBoxIcon.Error);

                }
            }
        }
        #endregion

        #region UI Interaction
        private void updateLinkLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            UpdatePreview(settingsProvider.ToString());
        }

        private void petCheckbox_CheckedChanged(object sender, EventArgs e)
        {
            Boolean enabled = petCheckbox.Checked;
            
            //petSpellTextbox.Enabled = enabled;
            petFociTextbox.Enabled = enabled;
            petShrinkCheckbox.Enabled = enabled;
            petShrinkSpellTextbox.Enabled = petShrinkCheckbox.Checked;
            petAggroCheckbox.Enabled = enabled;
            petAssistCheckbox.Enabled = enabled;

        }

        private void petShrinkCheckbox_CheckedChanged(object sender, EventArgs e)
        {
            petShrinkSpellTextbox.Enabled = petShrinkCheckbox.Checked;
        }

        private void forageCheckbox_CheckedChanged(object sender, EventArgs e)
        {
            forageIniTextbox.Enabled = forageCheckbox.Checked;
        }

        private void mountCheckbox_CheckedChanged(object sender, EventArgs e)
        {
            mountCastTextbox.Enabled = mountCheckbox.Checked;
        }

        


        private string filePath = string.Empty;
        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "ModBot Configuration File (MB_*.ini) | MB_*.ini";

            if (dialog.ShowDialog() == DialogResult.OK)
            {
                filePath = dialog.FileName;
                settingsProvider = Providers.ModBotSettingsProvider.LoadFromINI(filePath);
                LoadSettingsProvider(settingsProvider);
                this.Text = "ModBot Configuration Builder - v" + SharedLib.AssemblyVersion + " - " + dialog.FileName;
            }

        }
        #endregion

        Providers.ModBotSettingsProvider settingsProvider;
        public void LoadSettingsProvider(ModBotSettingsProvider settingsProvider)
        {
            
            meleeCheckbox.Checked = settingsProvider.DoMelee;
            healsCheckbox.Checked = settingsProvider.DoHeals;
            buffsCheckbox.Checked = settingsProvider.DoBuffs;
            debuffsCheckbox.Checked = settingsProvider.DoDebuffs;
            eventsCheckbox.Checked = settingsProvider.DoEvents;
            curesCheckbox.Checked = settingsProvider.DoCures;
            sitCheckbox.Checked = settingsProvider.DoSit;
            lootCheckbox.Checked = settingsProvider.DoLoot;
            afkCheckbox.Checked = settingsProvider.DoAfk;
            pullCheckbox.Checked = settingsProvider.DoPull;
            deathSlotCheckbox.Checked = settingsProvider.DeathSlot;
            fwCheckbox.Checked = settingsProvider.DoFW;
            
            petCheckbox.Checked = settingsProvider.DoPet;
            petSpellTextbox.Text = settingsProvider.PetCast;
            petFociTextbox.Text = settingsProvider.PetFoci;
            petShrinkCheckbox.Checked = settingsProvider.PetShrink;
            petShrinkSpellTextbox.Text = settingsProvider.PetShrinkSpell;
            petAggroCheckbox.Checked = settingsProvider.PetAggro;
            petAssistCheckbox.Checked = settingsProvider.PetAssist;

            forageCheckbox.Checked = settingsProvider.DoForage;
            forageIniTextbox.Text = settingsProvider.ForageIni;

            mountCheckbox.Checked = settingsProvider.DoMount;
            mountCastTextbox.Text = settingsProvider.MountCast;

            masterListTextbox.Text = settingsProvider.MasterList;
            tankNameTextbox.Text = settingsProvider.TankName;
            radiusTextbox.Text = settingsProvider.Radius.ToString();
            sitAggroTextbox.Text = settingsProvider.SitAggroRadiusCheck.ToString();
            afkMessageTextbox.Text = settingsProvider.AfkMessage;
            networkIniTextbox.Text = settingsProvider.NetworkIni;
            followStickTextbox.Text = settingsProvider.FollowStick;
            followDistanceTextbox.Text = settingsProvider.FollowDistance.ToString();

            LoadMelee(settingsProvider.MeleeSettingsProvider);
            LoadHeals(settingsProvider.HealSettingsProvider);
            LoadBuffs(settingsProvider.BuffSettingsProvider);
            LoadDebuffs(settingsProvider.DebuffSettingsProvider);
            LoadPull(settingsProvider.PullSettingsProvider);
            LoadEvents(settingsProvider.EventSettingsProvider);

            UpdatePreview(settingsProvider.ToString());
        }

        private void LoadMelee(Providers.MeleeSettingsProvider meleeSettingsProvider)
        {
            meleeOffTankCheckbox.Checked = meleeSettingsProvider.OffTank;
            meleeLeashTextbox.Text = meleeSettingsProvider.ACLeash.ToString();
            meleeAssistPctTextbox.Text = meleeSettingsProvider.ACAssistPct.ToString();
            meleeAnnounceTextbox.Text = meleeSettingsProvider.ACAnnounce;
            meleeMeleeTextbox.Text = meleeSettingsProvider.ACMeleeCmd;
            meleeBeforeTextbox.Text = meleeSettingsProvider.ACBefore;
            meleeAfterTextbox.Text = meleeSettingsProvider.ACAfter;
            meleeManaPctTextbox.Text = meleeSettingsProvider.ACManaPct.ToString();
        }

        private void LoadHeals(Providers.HealSettingsProvider healSettingsProvider)
        {
            healCountTextbox.Text = healSettingsProvider.AHCount.ToString();
            healCheckTimeTextbox.Text = healSettingsProvider.AHCheckTime.ToString();
            healOOBCCheckbox.Checked = healSettingsProvider.AHHealOOBC;
            healModeTextbox.Text = healSettingsProvider.AHHealMode;
            healInteruptLevelTextbox.Text = healSettingsProvider.AHInteruptLevel.ToString();
            healAllowDismountCheckbox.Checked = healSettingsProvider.AHAllowDismount;

            healsClassPriorityControl.SetClassPriority(healSettingsProvider.AHClassPriority, Globals.ClassPriorityDelimeter);
            
            healsDataGridView.DataSource = healSettingsProvider.HealSettingInstances;
        }

        private void LoadEvents(Providers.EventSettingsProvider eventSettingsProvider)
        {
            eventsCountTextbox.Text = eventSettingsProvider.AECount.ToString();
            eventsCheckTimeTextbox.Text = eventSettingsProvider.AECheckTime.ToString();
            eventsCustom1Textbox.Text = eventSettingsProvider.AECustom1;
            eventsCustom2Textbox.Text = eventSettingsProvider.AECustom2;
            eventsCustom3Textbox.Text = eventSettingsProvider.AECustom3;           
            
            eventInstanceDataGridView.DataSource = eventSettingsProvider.EventSettingInstances;
        }

        private void SetupEventsDataGridView()
        {
            eventInstanceDataGridView.AllowDrop = false;
            eventInstanceDataGridView.AllowUserToAddRows = false;
            eventInstanceDataGridView.AllowUserToDeleteRows = false;
            eventInstanceDataGridView.AllowUserToOrderColumns = false;
            eventInstanceDataGridView.AllowUserToResizeColumns = true;
            eventInstanceDataGridView.AllowUserToResizeRows = true;
            eventInstanceDataGridView.RowHeadersVisible = false;
            eventInstanceDataGridView.AutoGenerateColumns = false;
            eventInstanceDataGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.Fill);
            eventInstanceDataGridView.AutoResizeRows(DataGridViewAutoSizeRowsMode.AllCells);
            eventInstanceDataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            eventInstanceDataGridView.MultiSelect = false;

            eventInstanceDataGridView.Columns.Clear();

            /*DataGridViewTextBoxColumn indexColumn = new DataGridViewTextBoxColumn();
            indexColumn.DataPropertyName = "Index";
            indexColumn.HeaderText = "Index";
            indexColumn.ValueType = typeof(string);
            indexColumn.ReadOnly = true;
            eventInstanceDataGridView.Columns.Add(indexColumn);*/

            DataGridViewTextBoxColumn gemColumn = new DataGridViewTextBoxColumn();
            gemColumn.DataPropertyName = "AEGem";
            gemColumn.HeaderText = "Gem";
            gemColumn.ValueType = typeof(string);
            gemColumn.ReadOnly = true;
            eventInstanceDataGridView.Columns.Add(gemColumn);

            DataGridViewTextBoxColumn aliasColumn = new DataGridViewTextBoxColumn();
            aliasColumn.DataPropertyName = "AESpellAlias";
            aliasColumn.HeaderText = "Alias";
            aliasColumn.ValueType = typeof(string);
            aliasColumn.ReadOnly = true;
            eventInstanceDataGridView.Columns.Add(aliasColumn);

            DataGridViewTextBoxColumn spellColumn = new DataGridViewTextBoxColumn();
            spellColumn.DataPropertyName = "AESpell";
            spellColumn.HeaderText = "Spell";
            spellColumn.ValueType = typeof(string);
            spellColumn.ReadOnly = true;
            eventInstanceDataGridView.Columns.Add(spellColumn);

            eventInstanceDataGridView.DataSource = null;
        }



        private void SetupHealsDataGridView()
        {
            healsDataGridView.AllowDrop = false;
            healsDataGridView.AllowUserToAddRows = false;
            healsDataGridView.AllowUserToDeleteRows = false;
            healsDataGridView.AllowUserToOrderColumns = false;
            healsDataGridView.AllowUserToResizeColumns = true;
            healsDataGridView.AllowUserToResizeRows = true;
            healsDataGridView.RowHeadersVisible = false;
            healsDataGridView.AutoGenerateColumns = false;
            healsDataGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.Fill);
            healsDataGridView.AutoResizeRows(DataGridViewAutoSizeRowsMode.AllCells);
            healsDataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            healsDataGridView.MultiSelect = false;
            

            healsDataGridView.Columns.Clear();

            /*DataGridViewTextBoxColumn indexColumn = new DataGridViewTextBoxColumn();
            indexColumn.DataPropertyName = "Index";
            indexColumn.HeaderText = "Index";
            indexColumn.ValueType = typeof(string);
            indexColumn.ReadOnly = true;
            healsDataGridView.Columns.Add(indexColumn);*/

            DataGridViewTextBoxColumn gemColumn = new DataGridViewTextBoxColumn();
            gemColumn.DataPropertyName = "AHGem";
            gemColumn.HeaderText = "Gem";
            gemColumn.ValueType = typeof(string);
            gemColumn.ReadOnly = true;
            healsDataGridView.Columns.Add(gemColumn);

            DataGridViewTextBoxColumn aliasColumn = new DataGridViewTextBoxColumn();
            aliasColumn.DataPropertyName = "AHSpellAlias";
            aliasColumn.HeaderText = "Alias";
            aliasColumn.ValueType = typeof(string);
            aliasColumn.ReadOnly = true;
            healsDataGridView.Columns.Add(aliasColumn);

            DataGridViewTextBoxColumn spellColumn = new DataGridViewTextBoxColumn();
            spellColumn.DataPropertyName = "AHSpell";
            spellColumn.HeaderText = "Spell";
            spellColumn.ValueType = typeof(string);
            spellColumn.ReadOnly = true;
            healsDataGridView.Columns.Add(spellColumn);

            healsDataGridView.DataSource = null;
        }

        #region HealInstance Interaction
        private void healAddButton_Click(object sender, EventArgs e)
        {
            Providers.HealSettingInstanceProvider provider = new HealSettingInstanceProvider(settingsProvider.HealSettingsProvider,
                settingsProvider.HealSettingsProvider.AHCount + 1);
            HealInstanceForm form = new HealInstanceForm();
            form.LoadHealInstance(provider);

            form.ShowDialog(this);

            if (!form.IsCancelled)
            {
                settingsProvider.HealSettingsProvider.HealSettingInstances.Add(provider);
                healsDataGridView.DataSource = null;
                healsDataGridView.DataSource = settingsProvider.HealSettingsProvider.HealSettingInstances;
            }
        }

        private void healEditButton_Click(object sender, EventArgs e)
        {
            if (healsDataGridView.SelectedRows == null || healsDataGridView.SelectedRows.Count == 0) return;

            Providers.HealSettingInstanceProvider provider =
                (HealSettingInstanceProvider)healsDataGridView.SelectedRows[0].DataBoundItem;

            HealInstanceForm form = new HealInstanceForm();
            form.LoadHealInstance(provider);
            form.ShowDialog(this);

            if (!form.IsCancelled)
            {
                healsDataGridView.DataSource = null;
                healsDataGridView.DataSource = settingsProvider.HealSettingsProvider.HealSettingInstances;
            }
        }

        private void healDeleteButton_Click(object sender, EventArgs e)
        {
            if (healsDataGridView.SelectedRows == null || healsDataGridView.SelectedRows.Count == 0) return;

            Providers.HealSettingInstanceProvider provider =
                (HealSettingInstanceProvider)healsDataGridView.SelectedRows[0].DataBoundItem;

            if (SharedLib.GetDeleteConfirmation() == DialogResult.Yes)
            {
                settingsProvider.HealSettingsProvider.HealSettingInstances.Remove(provider);
                healsDataGridView.DataSource = null;
                healsDataGridView.DataSource = settingsProvider.HealSettingsProvider.HealSettingInstances;
            }

        }

        private void healsMoveUpButton_Click(object sender, EventArgs e)
        {
            if (healsDataGridView.SelectedRows == null || healsDataGridView.SelectedRows.Count == 0) return;

            Providers.HealSettingInstanceProvider selectedProvider =
                (HealSettingInstanceProvider)healsDataGridView.SelectedRows[0].DataBoundItem;
            int selectedIndex = healsDataGridView.SelectedRows[0].Index;

            if (selectedIndex == 0) return;

            Providers.HealSettingInstanceProvider neighborProvider = (HealSettingInstanceProvider)healsDataGridView.Rows[selectedIndex - 1].DataBoundItem;

            int neighborIndex = neighborProvider.Index;
            neighborProvider.Index = selectedProvider.Index;
            selectedProvider.Index = neighborIndex;

            settingsProvider.HealSettingsProvider.HealSettingInstances.Remove(selectedProvider);
            settingsProvider.HealSettingsProvider.HealSettingInstances.Insert(selectedIndex - 1, selectedProvider);

            healsDataGridView.DataSource = null;
            healsDataGridView.DataSource = settingsProvider.HealSettingsProvider.HealSettingInstances;
            healsDataGridView.Rows[selectedIndex - 1].Selected = true;

        }

        private void healsMoveDownButton_Click(object sender, EventArgs e)
        {
            if (healsDataGridView.SelectedRows == null || healsDataGridView.SelectedRows.Count == 0) return;

            Providers.HealSettingInstanceProvider selectedProvider =
                (HealSettingInstanceProvider)healsDataGridView.SelectedRows[0].DataBoundItem;
            int selectedIndex = healsDataGridView.SelectedRows[0].Index;

            if (selectedIndex == healsDataGridView.Rows.Count - 1) return;

            Providers.HealSettingInstanceProvider neighborProvider =
                (HealSettingInstanceProvider)healsDataGridView.Rows[selectedIndex + 1].DataBoundItem;

            int neighborIndex = neighborProvider.Index;
            neighborProvider.Index = selectedProvider.Index;
            selectedProvider.Index = neighborIndex;

            settingsProvider.HealSettingsProvider.HealSettingInstances.Remove(selectedProvider);
            settingsProvider.HealSettingsProvider.HealSettingInstances.Insert(selectedIndex + 1, selectedProvider);

            healsDataGridView.DataSource = null;
            healsDataGridView.DataSource = settingsProvider.HealSettingsProvider.HealSettingInstances;
            healsDataGridView.Rows[selectedIndex + 1].Selected = true;
        }
        #endregion


        private void LoadBuffs(Providers.BuffSettingsProvider buffSettingsProvider)
        {
            buffCountTextbox.Text = buffSettingsProvider.ABCount.ToString();
            buffCheckTimeTextbox.Text =buffSettingsProvider.ABCheckTime.ToString();
            buffMobMaxTextbox.Text = buffSettingsProvider.ABMobMax.ToString();

            buffInstanceDataGridView.DataSource = buffSettingsProvider.BuffSettingInstances;

        }

        private void SetupBuffsDataGridView()
        {
            buffInstanceDataGridView.AllowDrop = false;
            buffInstanceDataGridView.AllowUserToAddRows = false;
            buffInstanceDataGridView.AllowUserToDeleteRows = false;
            buffInstanceDataGridView.AllowUserToOrderColumns = false;
            buffInstanceDataGridView.AllowUserToResizeColumns = true;
            buffInstanceDataGridView.AllowUserToResizeRows = true;
            buffInstanceDataGridView.RowHeadersVisible = false;
            buffInstanceDataGridView.AutoGenerateColumns = false;
            buffInstanceDataGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.Fill);
            buffInstanceDataGridView.AutoResizeRows(DataGridViewAutoSizeRowsMode.AllCells);
            buffInstanceDataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            buffInstanceDataGridView.MultiSelect = false;

            buffInstanceDataGridView.Columns.Clear();

            /*DataGridViewTextBoxColumn indexColumn = new DataGridViewTextBoxColumn();
            indexColumn.DataPropertyName = "Index";
            indexColumn.HeaderText = "Index";
            indexColumn.ValueType = typeof(string);
            indexColumn.ReadOnly = true;
            buffInstanceDataGridView.Columns.Add(indexColumn);*/

            DataGridViewTextBoxColumn gemColumn = new DataGridViewTextBoxColumn();
            gemColumn.DataPropertyName = "ABGem";
            gemColumn.HeaderText = "Gem";
            gemColumn.ValueType = typeof(string);
            gemColumn.ReadOnly = true;
            buffInstanceDataGridView.Columns.Add(gemColumn);

            DataGridViewTextBoxColumn aliasColumn = new DataGridViewTextBoxColumn();
            aliasColumn.DataPropertyName = "ABSpellAlias";
            aliasColumn.HeaderText = "Alias";
            aliasColumn.ValueType = typeof(string);
            aliasColumn.ReadOnly = true;
            buffInstanceDataGridView.Columns.Add(aliasColumn);

            DataGridViewTextBoxColumn spellColumn = new DataGridViewTextBoxColumn();
            spellColumn.DataPropertyName = "ABSpell";
            spellColumn.HeaderText = "Spell";
            spellColumn.ValueType = typeof(string);
            spellColumn.ReadOnly = true;
            buffInstanceDataGridView.Columns.Add(spellColumn);

            buffInstanceDataGridView.DataSource = null;
        }

        #region Buff Instance Interaction
        private void buffInstanceAddButton_Click(object sender, EventArgs e)
        {
            Providers.BuffSettingInstanceProvider provider = new BuffSettingInstanceProvider(settingsProvider.BuffSettingsProvider,
                settingsProvider.BuffSettingsProvider.ABCount + 1);
            BuffInstanceForm form = new BuffInstanceForm();
            form.LoadBuffInstance(provider);            
            form.ShowDialog(this);

            if (!form.IsCancelled)
            {
                settingsProvider.BuffSettingsProvider.BuffSettingInstances.Add(provider);
                buffInstanceDataGridView.DataSource = null;
                buffInstanceDataGridView.DataSource = settingsProvider.BuffSettingsProvider.BuffSettingInstances;
            }
        }

        private void buffInstanceEditButton_Click(object sender, EventArgs e)
        {
            if (buffInstanceDataGridView.SelectedRows == null || buffInstanceDataGridView.SelectedRows.Count == 0) return;

            Providers.BuffSettingInstanceProvider provider =
                (BuffSettingInstanceProvider)buffInstanceDataGridView.SelectedRows[0].DataBoundItem;

            BuffInstanceForm form = new BuffInstanceForm();
            form.LoadBuffInstance(provider);
            form.ShowDialog(this);

            if (!form.IsCancelled)
            {
                buffInstanceDataGridView.DataSource = null;
                buffInstanceDataGridView.DataSource = settingsProvider.BuffSettingsProvider.BuffSettingInstances;
            }
        }

        private void buffInstanceDeleteButton_Click(object sender, EventArgs e)
        {
            if (buffInstanceDataGridView.SelectedRows == null || buffInstanceDataGridView.SelectedRows.Count == 0) return;

            Providers.BuffSettingInstanceProvider provider =
                (BuffSettingInstanceProvider)buffInstanceDataGridView.SelectedRows[0].DataBoundItem;

            if (SharedLib.GetDeleteConfirmation() == DialogResult.Yes)
            {
                settingsProvider.BuffSettingsProvider.BuffSettingInstances.Remove(provider);
                buffInstanceDataGridView.DataSource = null;
                buffInstanceDataGridView.DataSource = settingsProvider.BuffSettingsProvider.BuffSettingInstances;
            }

        }

        private void buffInstanceMoveDownButton_Click(object sender, EventArgs e)
        {
            if (buffInstanceDataGridView.SelectedRows == null || buffInstanceDataGridView.SelectedRows.Count == 0) return;

            Providers.BuffSettingInstanceProvider selectedProvider =
                (BuffSettingInstanceProvider)buffInstanceDataGridView.SelectedRows[0].DataBoundItem;
            int selectedIndex = buffInstanceDataGridView.SelectedRows[0].Index;

            if (selectedIndex == buffInstanceDataGridView.Rows.Count - 1) return;

            Providers.BuffSettingInstanceProvider neighborProvider =
                (BuffSettingInstanceProvider)buffInstanceDataGridView.Rows[selectedIndex + 1].DataBoundItem;

            int neighborIndex = neighborProvider.Index;
            neighborProvider.Index = selectedProvider.Index;
            selectedProvider.Index = neighborIndex;

            settingsProvider.BuffSettingsProvider.BuffSettingInstances.Remove(selectedProvider);
            settingsProvider.BuffSettingsProvider.BuffSettingInstances.Insert(selectedIndex + 1, selectedProvider);

            buffInstanceDataGridView.DataSource = null;
            buffInstanceDataGridView.DataSource = settingsProvider.BuffSettingsProvider.BuffSettingInstances;
            buffInstanceDataGridView.Rows[selectedIndex + 1].Selected = true;
        }

        private void buffInstanceMoveUpButton_Click(object sender, EventArgs e)
        {
            if (buffInstanceDataGridView.SelectedRows == null || buffInstanceDataGridView.SelectedRows.Count == 0) return;

            Providers.BuffSettingInstanceProvider selectedProvider =
                (BuffSettingInstanceProvider)buffInstanceDataGridView.SelectedRows[0].DataBoundItem;
            int selectedIndex = buffInstanceDataGridView.SelectedRows[0].Index;

            if (selectedIndex == 0) return;

            Providers.BuffSettingInstanceProvider neighborProvider = (BuffSettingInstanceProvider)buffInstanceDataGridView.Rows[selectedIndex - 1].DataBoundItem;

            int neighborIndex = neighborProvider.Index;
            neighborProvider.Index = selectedProvider.Index;
            selectedProvider.Index = neighborIndex;

            settingsProvider.BuffSettingsProvider.BuffSettingInstances.Remove(selectedProvider);
            settingsProvider.BuffSettingsProvider.BuffSettingInstances.Insert(selectedIndex - 1, selectedProvider);

            buffInstanceDataGridView.DataSource = null;
            buffInstanceDataGridView.DataSource = settingsProvider.BuffSettingsProvider.BuffSettingInstances;
            buffInstanceDataGridView.Rows[selectedIndex - 1].Selected = true;
        }
        #endregion


        private void LoadDebuffs(Providers.DebuffSettingsProvider debuffSettingsProvider)
        {
            debuffCountTextbox.Text = debuffSettingsProvider.ADCount.ToString();
            debuffCheckTimeTextbox.Text = debuffSettingsProvider.ADCheckTime.ToString();
            debuffMobMaxTextbox.Text = debuffSettingsProvider.ADMobMax.ToString();
            debuffAggroOnlyCheckbox.Checked = debuffSettingsProvider.ADAggroOnly;
            debuffHoldTextbox.Text = debuffSettingsProvider.ADHold.ToString();

            debuffInstanceDataGridView.DataSource = debuffSettingsProvider.DebuffSettingInstances;

        }

        private void SetupDebuffsDataGridView()
        {
            debuffInstanceDataGridView.AllowDrop = false;
            debuffInstanceDataGridView.AllowUserToAddRows = false;
            debuffInstanceDataGridView.AllowUserToDeleteRows = false;
            debuffInstanceDataGridView.AllowUserToOrderColumns = false;
            debuffInstanceDataGridView.AllowUserToResizeColumns = true;
            debuffInstanceDataGridView.AllowUserToResizeRows = true;
            debuffInstanceDataGridView.RowHeadersVisible = false;
            debuffInstanceDataGridView.AutoGenerateColumns = false;
            debuffInstanceDataGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.Fill);
            debuffInstanceDataGridView.AutoResizeRows(DataGridViewAutoSizeRowsMode.AllCells);
            debuffInstanceDataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            debuffInstanceDataGridView.MultiSelect = false;

            debuffInstanceDataGridView.Columns.Clear();

            /*DataGridViewTextBoxColumn indexColumn = new DataGridViewTextBoxColumn();
            indexColumn.DataPropertyName = "Index";
            indexColumn.HeaderText = "Index";
            indexColumn.ValueType = typeof(string);
            indexColumn.ReadOnly = true;
            debuffInstanceDataGridView.Columns.Add(indexColumn);*/

            DataGridViewTextBoxColumn gemColumn = new DataGridViewTextBoxColumn();
            gemColumn.DataPropertyName = "ADGem";
            gemColumn.HeaderText = "Gem";
            gemColumn.ValueType = typeof(string);
            gemColumn.ReadOnly = true;
            debuffInstanceDataGridView.Columns.Add(gemColumn);

            DataGridViewTextBoxColumn aliasColumn = new DataGridViewTextBoxColumn();
            aliasColumn.DataPropertyName = "ADSpellAlias";
            aliasColumn.HeaderText = "Alias";
            aliasColumn.ValueType = typeof(string);
            aliasColumn.ReadOnly = true;
            debuffInstanceDataGridView.Columns.Add(aliasColumn);

            DataGridViewTextBoxColumn spellColumn = new DataGridViewTextBoxColumn();
            spellColumn.DataPropertyName = "ADSpell";
            spellColumn.HeaderText = "Spell";
            spellColumn.ValueType = typeof(string);
            spellColumn.ReadOnly = true;
            debuffInstanceDataGridView.Columns.Add(spellColumn);

            debuffInstanceDataGridView.DataSource = null;
        }

        #region Debuff Instance Interaction
        private void debuffInstanceAddButton_Click(object sender, EventArgs e)
        {
            Providers.DebuffSettingInstanceProvider provider = new DebuffSettingInstanceProvider(settingsProvider.DebuffSettingsProvider,
                settingsProvider.DebuffSettingsProvider.ADCount + 1);
            DebuffInstanceForm form = new DebuffInstanceForm();
            form.LoadDebuffInstance(provider);
            form.ShowDialog(this);

            if (!form.IsCancelled)
            {
                settingsProvider.DebuffSettingsProvider.DebuffSettingInstances.Add(provider);
                debuffInstanceDataGridView.DataSource = null;
                debuffInstanceDataGridView.DataSource = settingsProvider.DebuffSettingsProvider.DebuffSettingInstances;
            }
        }

        private void debuffInstanceEditButton_Click(object sender, EventArgs e)
        {
            if (debuffInstanceDataGridView.SelectedRows == null || debuffInstanceDataGridView.SelectedRows.Count == 0) return;

            Providers.DebuffSettingInstanceProvider provider =
                (DebuffSettingInstanceProvider)debuffInstanceDataGridView.SelectedRows[0].DataBoundItem;

            DebuffInstanceForm form = new DebuffInstanceForm();
            form.LoadDebuffInstance(provider);
            form.ShowDialog(this);

            if (!form.IsCancelled)
            {
                debuffInstanceDataGridView.DataSource = null;
                debuffInstanceDataGridView.DataSource = settingsProvider.DebuffSettingsProvider.DebuffSettingInstances;
            }
        }

        private void debuffInstanceDeleteButton_Click(object sender, EventArgs e)
        {
            if (debuffInstanceDataGridView.SelectedRows == null || debuffInstanceDataGridView.SelectedRows.Count == 0) return;

            Providers.DebuffSettingInstanceProvider provider =
                (DebuffSettingInstanceProvider)debuffInstanceDataGridView.SelectedRows[0].DataBoundItem;

            if (SharedLib.GetDeleteConfirmation() == DialogResult.Yes)
            {
                settingsProvider.DebuffSettingsProvider.DebuffSettingInstances.Remove(provider);
                debuffInstanceDataGridView.DataSource = null;
                debuffInstanceDataGridView.DataSource = settingsProvider.DebuffSettingsProvider.DebuffSettingInstances;
            }

        }

        private void debuffInstanceMoveDownButton_Click(object sender, EventArgs e)
        {
            if (debuffInstanceDataGridView.SelectedRows == null || debuffInstanceDataGridView.SelectedRows.Count == 0) return;

            Providers.DebuffSettingInstanceProvider selectedProvider =
                (DebuffSettingInstanceProvider)debuffInstanceDataGridView.SelectedRows[0].DataBoundItem;
            int selectedIndex = debuffInstanceDataGridView.SelectedRows[0].Index;

            if (selectedIndex == debuffInstanceDataGridView.Rows.Count - 1) return;

            Providers.DebuffSettingInstanceProvider neighborProvider =
                (DebuffSettingInstanceProvider)debuffInstanceDataGridView.Rows[selectedIndex + 1].DataBoundItem;

            int neighborIndex = neighborProvider.Index;
            neighborProvider.Index = selectedProvider.Index;
            selectedProvider.Index = neighborIndex;

            settingsProvider.DebuffSettingsProvider.DebuffSettingInstances.Remove(selectedProvider);
            settingsProvider.DebuffSettingsProvider.DebuffSettingInstances.Insert(selectedIndex + 1, selectedProvider);

            debuffInstanceDataGridView.DataSource = null;
            debuffInstanceDataGridView.DataSource = settingsProvider.DebuffSettingsProvider.DebuffSettingInstances;
            debuffInstanceDataGridView.Rows[selectedIndex + 1].Selected = true;
        }

        private void debuffInstanceMoveUpButton_Click(object sender, EventArgs e)
        {
            if (debuffInstanceDataGridView.SelectedRows == null || debuffInstanceDataGridView.SelectedRows.Count == 0) return;

            Providers.DebuffSettingInstanceProvider selectedProvider =
                (DebuffSettingInstanceProvider)debuffInstanceDataGridView.SelectedRows[0].DataBoundItem;
            int selectedIndex = debuffInstanceDataGridView.SelectedRows[0].Index;

            if (selectedIndex == 0) return;

            Providers.DebuffSettingInstanceProvider neighborProvider = (DebuffSettingInstanceProvider)debuffInstanceDataGridView.Rows[selectedIndex - 1].DataBoundItem;

            int neighborIndex = neighborProvider.Index;
            neighborProvider.Index = selectedProvider.Index;
            selectedProvider.Index = neighborIndex;

            settingsProvider.DebuffSettingsProvider.DebuffSettingInstances.Remove(selectedProvider);
            settingsProvider.DebuffSettingsProvider.DebuffSettingInstances.Insert(selectedIndex - 1, selectedProvider);

            debuffInstanceDataGridView.DataSource = null;
            debuffInstanceDataGridView.DataSource = settingsProvider.DebuffSettingsProvider.DebuffSettingInstances;
            debuffInstanceDataGridView.Rows[selectedIndex - 1].Selected = true;
        }
        #endregion

        private void LoadPull(Providers.PullSettingsProvider pullSettingsProvider)
        {
            pullCheckTimeTextbox.Text = pullSettingsProvider.APCheckTime.ToString();
            pullRadiusTextbox.Text = pullSettingsProvider.APRadius.ToString();
            pullMobMaxTextbox.Text = pullSettingsProvider.APMobMax.ToString();
            pullScriptTextbox.Text = pullSettingsProvider.APScript;
            pullPathTextbox.Text = pullSettingsProvider.APPath;
            pullAnnounceTextbox.Text = pullSettingsProvider.APAnnounce;
            pullBeforeTextbox.Text = pullSettingsProvider.APBefore;
            pullAfterTextbox.Text = pullSettingsProvider.APAfter;
        }

        #region Menu Bar Controls
        private void modBotWikiToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SharedLib.NavigateUrl("http://www.macroquest2.com/wiki/index.php/ModBot");
        }

        private void modBotClassExamplesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SharedLib.NavigateUrl("http://www.macroquest2.com/wiki/index.php/Modbot_Class_INI_Examples");
        }

        private void modBotForumDiscussionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SharedLib.NavigateUrl("http://www.macroquest2.com/phpBB3/viewtopic.php?f=49&t=15313");
        }

        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.filePath = string.Empty;
            settingsProvider = new ModBot.ConfigBuilder.Providers.ModBotSettingsProvider();
            LoadSettingsProvider(settingsProvider);
        }

        private void lucySpellDownloadsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SharedLib.NavigateUrl("http://lucy.allakhazam.com/downloads.html");
        }

        private string spellDatabaseFilepath = string.Empty;
        private void setSpellDatabaseFileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "Spell Data File (spelldata_*.txt) | spelldata_*.txt";

            if (dialog.ShowDialog() == DialogResult.OK)
            {
                spellDatabaseFilepath = dialog.FileName;

                try
                {
                    Globals.SpellDatabaseProvider = SpellDatabaseProvider.ParseSpellsFromFile(spellDatabaseFilepath);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error parsing spell database. Please ensure you have selected a valid database file for loading.\r\n\r\n" + ex.Message, 
                        "Parser error", MessageBoxButtons.OK, MessageBoxIcon.Error);

                }
                SharedLib.SpellFilePath = spellDatabaseFilepath;
            }
        }
        #endregion        

        private void SaveSettingsProviderInformation()
        {
            try
            {
                settingsProvider.DoMelee = meleeCheckbox.Checked;
                settingsProvider.DoHeals = healsCheckbox.Checked;
                settingsProvider.DoBuffs = buffsCheckbox.Checked;
                settingsProvider.DoDebuffs = debuffsCheckbox.Checked;
                settingsProvider.DoEvents = eventsCheckbox.Checked;
                settingsProvider.DoCures = curesCheckbox.Checked;
                settingsProvider.DoSit = sitCheckbox.Checked;
                settingsProvider.DoLoot = lootCheckbox.Checked;
                settingsProvider.DoAfk = afkCheckbox.Checked;
                settingsProvider.DoPull = pullCheckbox.Checked;
                settingsProvider.DeathSlot = deathSlotCheckbox.Checked;
                settingsProvider.DoFW = fwCheckbox.Checked;

                settingsProvider.DoPet = petCheckbox.Checked;
                settingsProvider.PetCast = petSpellTextbox.Text;
                settingsProvider.PetFoci = petFociTextbox.Text;
                settingsProvider.PetShrink = petShrinkCheckbox.Checked;
                settingsProvider.PetShrinkSpell = petShrinkSpellTextbox.Text;
                settingsProvider.PetAggro = petAggroCheckbox.Checked;
                settingsProvider.PetAssist = petAssistCheckbox.Checked;

                settingsProvider.DoForage = forageCheckbox.Checked;
                settingsProvider.ForageIni = forageIniTextbox.Text;

                settingsProvider.DoMount = mountCheckbox.Checked;
                settingsProvider.MountCast = mountCastTextbox.Text;

                settingsProvider.MasterList = masterListTextbox.Text;
                settingsProvider.TankName = tankNameTextbox.Text;
                settingsProvider.Radius = Convert.ToInt32(radiusTextbox.Text);
                settingsProvider.SitAggroRadiusCheck = Convert.ToInt32(sitAggroTextbox.Text);
                settingsProvider.AfkMessage = afkMessageTextbox.Text;
                settingsProvider.NetworkIni = networkIniTextbox.Text;
                settingsProvider.FollowStick = followStickTextbox.Text;
                settingsProvider.FollowDistance = Convert.ToInt32(followDistanceTextbox.Text);

                SaveMelee(settingsProvider.MeleeSettingsProvider);
                SaveHeals(settingsProvider.HealSettingsProvider);
                SaveBuffs(settingsProvider.BuffSettingsProvider);
                SaveDebuffs(settingsProvider.DebuffSettingsProvider);
                SavePull(settingsProvider.PullSettingsProvider);
                SaveEvents(settingsProvider.EventSettingsProvider);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error encountered parsing form values.\r\n\r\n" + ex.Message, "General Save error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        private void SaveMelee(MeleeSettingsProvider meleeSettingsProvider)
        {
            meleeSettingsProvider.OffTank = meleeOffTankCheckbox.Checked;
            meleeSettingsProvider.ACLeash = Convert.ToInt32(meleeLeashTextbox.Text);
            meleeSettingsProvider.ACAssistPct = Convert.ToInt32(meleeAssistPctTextbox.Text);
            meleeSettingsProvider.ACAnnounce = meleeAnnounceTextbox.Text;
            meleeSettingsProvider.ACMeleeCmd = meleeMeleeTextbox.Text;
            meleeSettingsProvider.ACBefore = meleeBeforeTextbox.Text;
            meleeSettingsProvider.ACAfter = meleeAfterTextbox.Text;
            meleeSettingsProvider.ACManaPct = Convert.ToInt32(meleeManaPctTextbox.Text);
        }

        private void SaveHeals(HealSettingsProvider healSettingsProvider)
        {

            healSettingsProvider.AHCount = Convert.ToInt32(healCountTextbox.Text);
            healSettingsProvider.AHCheckTime = Convert.ToInt32(healCheckTimeTextbox.Text);
            healSettingsProvider.AHHealOOBC = healOOBCCheckbox.Checked;
            healSettingsProvider.AHHealMode = healModeTextbox.Text;
            healSettingsProvider.AHInteruptLevel = Convert.ToInt32(healInteruptLevelTextbox.Text);
            healSettingsProvider.AHAllowDismount = healAllowDismountCheckbox.Checked;

            healSettingsProvider.AHClassPriority = healsClassPriorityControl.GetClassPriority(' ');
        }

        private void SaveBuffs(BuffSettingsProvider buffSettingsProvider)
        {
            buffSettingsProvider.ABCount = Convert.ToInt32(buffCountTextbox.Text);
            buffSettingsProvider.ABCheckTime = Convert.ToInt32(buffCheckTimeTextbox.Text);
            buffSettingsProvider.ABMobMax = Convert.ToInt32(buffMobMaxTextbox.Text);

        }

        private void SaveDebuffs(DebuffSettingsProvider debuffSettingsProvider)
        {
            debuffSettingsProvider.ADCount = Convert.ToInt32(debuffCountTextbox.Text);
            debuffSettingsProvider.ADCheckTime = Convert.ToInt32(debuffCheckTimeTextbox.Text);
            debuffSettingsProvider.ADMobMax = Convert.ToInt32(debuffMobMaxTextbox.Text);
            debuffSettingsProvider.ADAggroOnly = debuffAggroOnlyCheckbox.Checked;
            debuffSettingsProvider.ADHold = debuffHoldTextbox.Text;
        }

        private void SavePull(PullSettingsProvider pullSettingsProvider)
        {
            pullSettingsProvider.APCheckTime = Convert.ToInt32(pullCheckTimeTextbox.Text);
            pullSettingsProvider.APRadius = Convert.ToInt32(pullRadiusTextbox.Text);
            pullSettingsProvider.APMobMax = Convert.ToInt32(pullMobMaxTextbox.Text);
            pullSettingsProvider.APScript = pullScriptTextbox.Text;
            pullSettingsProvider.APPath = pullPathTextbox.Text;
            pullSettingsProvider.APAnnounce = pullAnnounceTextbox.Text;
            pullSettingsProvider.APBefore = pullBeforeTextbox.Text;
            pullSettingsProvider.APAfter = pullAfterTextbox.Text;
        }

        private void SaveEvents(EventSettingsProvider eventSettingsProvider)
        {
            eventSettingsProvider.AECount = Convert.ToInt32(eventsCountTextbox.Text);
            eventSettingsProvider.AECheckTime = Convert.ToInt32(eventsCheckTimeTextbox.Text);
            eventSettingsProvider.AECustom1 = eventsCustom1Textbox.Text;
            eventSettingsProvider.AECustom2 = eventsCustom2Textbox.Text;
            eventSettingsProvider.AECustom3 = eventsCustom3Textbox.Text;

        }

        private void UpdatePreview(string previewText)
        {
            previewTextbox.Text = previewText;
            Application.DoEvents();
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.filePath == null || this.filePath.Length == 0)
                SaveAs();
            else
            {
                Save(filePath);
            }

        }

        private void SaveAs()
        {
            SaveFileDialog saveDialog = new SaveFileDialog();
            saveDialog.Filter = "ModBot Configuration File (MB_*.ini) | MB_*.ini";
            if (saveDialog.ShowDialog() == DialogResult.OK)
            {
                this.filePath = saveDialog.FileName;
                Save(filePath);
            }
        }

        private void Save(string filepath)
        {
            using (System.IO.StreamWriter writer = new System.IO.StreamWriter(filePath, false))
            {
                writer.Write(this.settingsProvider.ToString());
                writer.Flush();
            }
        }

        private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveAs();
        }

        
    }
}
